home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Double.java < prev    next >
Text File  |  1998-09-22  |  14KB  |  394 lines

  1. /*
  2.  * @(#)Double.java    1.42 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Double class wraps a value of the primitive type 
  19.  * <code>double</code> in an object. An object of type 
  20.  * <code>Double</code> contains a single field whose type is 
  21.  * <code>double</code>. 
  22.  * <p>
  23.  * In addition, this class provides several methods for converting a 
  24.  * <code>double</code> to a <code>String</code> and a 
  25.  * <code>String</code> to a <code>double</code>, as well as other 
  26.  * constants and methods useful when dealing with a 
  27.  * <code>double</code>. 
  28.  *
  29.  * @author  Lee Boynton
  30.  * @author  Arthur van Hoff
  31.  * @version 1.42, 07/01/98
  32.  * @since   JDK1.0
  33.  */
  34. public final
  35. class Double extends Number {
  36.     /**
  37.      * The positive infinity of type <code>double</code>. 
  38.      *
  39.      * @since   JDK1.0
  40.      */
  41.     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
  42.  
  43.     /**
  44.      * The negative infinity of type <code>double</code>. 
  45.      *
  46.      * @since   JDK1.0
  47.      */
  48.     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  49.  
  50.     /** 
  51.      * A NaN value of type <code>double</code>. 
  52.      *
  53.      * @since   JDK1.0
  54.      */
  55.     public static final double NaN = 0.0d / 0.0;
  56.  
  57.     /**
  58.      * The largest positive value of type <code>double</code>. 
  59.      *
  60.      * @since   JDK1.0
  61.      */
  62.     public static final double MAX_VALUE = 1.79769313486231570e+308;
  63.  
  64.     /**
  65.      * The smallest positive value of type <code>double</code>. 
  66.      *
  67.      * @since   JDK1.0
  68.      */
  69. //  public static final double MIN_VALUE = 4.94065645841246544e-324;
  70.     public static final double MIN_VALUE = longBitsToDouble(1L);
  71.  
  72.     /**
  73.      * The Class object representing the primitive type double.
  74.      *
  75.      * @since   JDK1.1
  76.      */
  77.     public static final Class    TYPE = Class.getPrimitiveClass("double");
  78.  
  79.     /**
  80.      * Creates a string representation of the <code>double</code> 
  81.      * argument. 
  82.      * <p>
  83.      * The values <code>NaN</code>, <code>NEGATIVE_INFINITY</code>, 
  84.      * <code>POSITIVE_INFINITY</code>, <code>-0.0</code>, and 
  85.      * <code>+0.0</code> are represented by the strings 
  86.      * <code>"NaN"</code>, <code>"-Infinity"</code>, 
  87.      * <code>"Infinity"</code>, <code>"-0.0"</code>, and 
  88.      * <code>"0.0"</code>, respectively. 
  89.      * <p>
  90.      * If <code>d</code> is in the range 
  91.      * <code>10<sup>-3</sup> <= |d| <=10<sup>7</sup></code>,
  92.      * then it is converted to a string in the style 
  93.      * <code>[-]ddd.ddd</code>. Otherwise, it is converted to a 
  94.      * string in the style <code>[-]m.ddddE±xx</code>.
  95.      * <p>
  96.      * There is always a minimum of one digit after the decimal point. 
  97.      * The number of digits is the minimum needed to uniquely distinguish 
  98.      * the argument value from adjacent values of type 
  99.      * <code>double</code>. 
  100.      *
  101.      * @param   d   the double to be converted.
  102.      * @return  a string representation of the argument.
  103.      * @since   JDK1.0
  104.      */
  105.     public static String toString(double d){
  106.     return new FloatingDecimal(d).toJavaFormatString();
  107.     }
  108.  
  109.     /**
  110.      * Returns a new Double value initialized to the value represented by the 
  111.      * specified String.
  112.      *
  113.      * @param      s   the string to be parsed.
  114.      * @return     a newly constructed <code>Double</code> initialized to the
  115.      *             value represented by the string argument.
  116.      * @exception  NumberFormatException  if the string does not contain a
  117.      *               parsable number.
  118.      * @since      JDK1.0
  119.      */
  120.     public static Double valueOf(String s) throws NumberFormatException { 
  121.     return new Double(valueOf0(s));
  122.     }
  123.  
  124.     /**
  125.      * Returns true if the specified number is the special Not-a-Number (NaN)
  126.      * value.
  127.      *
  128.      * @param   v   the value to be tested.
  129.      * @return  <code>true</code> if the value of the argument is NaN;
  130.      *          <code>false</code> otherwise.
  131.      * @since   JDK1.0
  132.      */
  133.     static public boolean isNaN(double v) {
  134.     return (v != v);
  135.     }
  136.  
  137.     /**
  138.      * Returns true if the specified number is infinitely large in magnitude.
  139.      *
  140.      * @param   v   the value to be tested.
  141.      * @return  <code>true</code> if the value of the argument is positive
  142.      *          infinity or negative infinity; <code>false</code> otherwise.
  143.      * @since   JDK1.0
  144.      */
  145.     static public boolean isInfinite(double v) {
  146.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  147.     }
  148.  
  149.     /**
  150.      * The value of the Double.
  151.      */
  152.     private double value;
  153.  
  154.     /**
  155.      * Constructs a newly allocated <code>Double</code> object that 
  156.      * represents the primitive <code>double</code> argument. 
  157.      *
  158.      * @param   value   the value to be represented by the <code>Double</code>.
  159.      * @since   JDK1.0
  160.      */
  161.     public Double(double value) {
  162.     this.value = value;
  163.     }
  164.  
  165.     /**
  166.      * Constructs a newly allocated <code>Double</code> object that 
  167.      * represents the floating- point value of type <code>double</code> 
  168.      * represented by the string. The string is converted to a 
  169.      * <code>double</code> value as if by the <code>valueOf</code> method. 
  170.      *
  171.      * @param      s   a string to be converted to a <code>Double</code>.
  172.      * @exception  NumberFormatException  if the string does not contain a
  173.      *               parsable number.
  174.      * @see        java.lang.Double#valueOf(java.lang.String)
  175.      * @since      JDK1.0
  176.      */
  177.     public Double(String s) throws NumberFormatException {
  178.     // REMIND: this is inefficient
  179.     this(valueOf(s).doubleValue());
  180.     }
  181.  
  182.     /**
  183.      * Returns true if this Double value is the special Not-a-Number (NaN)
  184.      * value.
  185.      *
  186.      * @return  <code>true</code> if the value represented by this object is
  187.      *          NaN; <code>false</code> otherwise.
  188.      * @since   JDK1.0
  189.      */
  190.     public boolean isNaN() {
  191.     return isNaN(value);
  192.     }
  193.  
  194.     /**
  195.      * Returns true if this Double value is infinitely large in magnitude.
  196.      *
  197.      * @return  <code>true</code> if the value represented by this object is
  198.      *          positive infinity or negative infinity;
  199.      *          <code>false</code> otherwise.
  200.      * @since   JDK1.0
  201.      */
  202.     public boolean isInfinite() {
  203.     return isInfinite(value);
  204.     }
  205.  
  206.     /**
  207.      * Returns a String representation of this Double object.
  208.      * The primitive <code>double</code> value represented by this 
  209.      * object is converted to a string exactly as if by the method 
  210.      * <code>toString</code> of one argument. 
  211.      *
  212.      * @return  a <code>String</code> representation of this object.
  213.      * @see     java.lang.Double#toString(double)
  214.      * @since   JDK1.0
  215.      */
  216.     public String toString() {
  217.     return String.valueOf(value);
  218.     }
  219.  
  220.     /**
  221.      * Returns the value of this Double as a byte (by casting to a byte).
  222.      *
  223.      * @since   JDK1.1
  224.      */
  225.     public byte byteValue() {
  226.     return (byte)value;
  227.     }
  228.  
  229.     /**
  230.      * Returns the value of this Double as a short (by casting to a short).
  231.      *
  232.      * @since   JDK1.1
  233.      */
  234.     public short shortValue() {
  235.     return (short)value;
  236.     }
  237.  
  238.     /**
  239.      * Returns the integer value of this Double (by casting to an int).
  240.      *
  241.      * @return  the <code>double</code> value represented by this object is
  242.      *          converted to type <code>int</code> and the result of the
  243.      *          conversion is returned.
  244.      * @since   JDK1.0
  245.      */
  246.     public int intValue() {
  247.     return (int)value;
  248.     }
  249.  
  250.     /**
  251.      * Returns the long value of this Double (by casting to a long).
  252.      *
  253.      * @return  the <code>double</code> value represented by this object is
  254.      *          converted to type <code>long</code> and the result of the
  255.      *          conversion is returned.
  256.      * @since   JDK1.0
  257.      */
  258.     public long longValue() {
  259.     return (long)value;
  260.     }
  261.  
  262.     /**
  263.      * Returns the float value of this Double.
  264.      *
  265.      * @return  the <code>double</code> value represented by this object is
  266.      *          converted to type <code>float</code> and the result of the
  267.      *          conversion is returned.
  268.      * @since   JDK1.0     */
  269.     public float floatValue() {
  270.     return (float)value;
  271.     }
  272.  
  273.     /**
  274.      * Returns the double value of this Double.
  275.      *
  276.      * @return  the <code>double</code> value represented by this object.
  277.      * @since   JDK1.0
  278.      */
  279.     public double doubleValue() {
  280.     return (double)value;
  281.     }
  282.  
  283.     /**
  284.      * Returns a hashcode for this Double.
  285.      *
  286.      * @return  a <code>hash code</code> value for this object. 
  287.      * @since   JDK1.0
  288.      */
  289.     public int hashCode() {
  290.     long bits = doubleToLongBits(value);
  291.     return (int)(bits ^ (bits >> 32));
  292.     }
  293.  
  294.     /**
  295.      * Compares this object against the specified object.
  296.      * The result is <code>true</code> if and only if the argument is 
  297.      * not <code>null</code> and is a <code>Double</code> object that 
  298.      * represents a double that has the identical bit pattern to the bit 
  299.      * pattern of the double represented by this object. 
  300.      * <p>
  301.      * Note that in most cases, for two instances of class 
  302.      * <code>Double</code>, <code>d1</code> and <code>d2</code>, the 
  303.      * value of <code>d1.equals(d2)</code> is <code>true</code> if and 
  304.      * only if 
  305.      * <ul><code>
  306.      *   d1.doubleValue() == d2.doubleValue()
  307.      * </code></ul>
  308.      * <p>
  309.      * also has the value <code>true</code>. However, there are two 
  310.      * exceptions: 
  311.      * <ul>
  312.      * <li>If <code>d1</code> and <code>d2</code> both represent 
  313.      *     <code>Double.NaN</code>, then the <code>equals</code> method 
  314.      *     returns <code>true</code>, even though 
  315.      *     <code>Double.NaN==Double.NaN</code> has the value 
  316.      *     <code>false</code>.
  317.      * <li>If <code>d1</code> represents <code>+0.0</code> while
  318.      *     <code>d2</code> represents <code>-0.0</code>, or vice versa,
  319.      *     the <code>equal</code> test has the value <code>false</code>,
  320.      *     even though <code>+0.0==-0.0</code> has the value <code>true.</code>
  321.      * </ul>
  322.      *
  323.      * @param   obj   the object to compare with.
  324.      * @return  <code>true</code> if the objects are the same;
  325.      *          <code>false</code> otherwise.
  326.      * @since   JDK1.0
  327.      */
  328.     public boolean equals(Object obj) {
  329.     return (obj != null)
  330.            && (obj instanceof Double) 
  331.            && (doubleToLongBits(((Double)obj).value) == 
  332.               doubleToLongBits(value));
  333.     }
  334.  
  335.     /**
  336.      * Returns a representation of the specified floating-point value 
  337.      * according to the IEEE 754 floating-point "double 
  338.      * format" bit layout. 
  339.      * <p>
  340.      * Bit 63 represents the sign of the floating-point number. Bits 
  341.      * 62-52 represent the exponent. Bits 51-0 represent 
  342.      * the significand (sometimes called the mantissa) of the 
  343.      * floating-point number. 
  344.      * <p>
  345.      * If the argument is positive infinity, the result is 
  346.      * <code>0x7ff0000000000000L</code>. 
  347.      * <p>
  348.      * If the argument is negative infinity, the result is 
  349.      * <code>0xfff0000000000000L</code>. 
  350.      * <p>
  351.      * If the argument is NaN, the result is 
  352.      * <code>0x7ff8000000000000L</code>. 
  353.      *
  354.      * @param   value   a double precision floating-point number.
  355.      * @return  the bits that represent the floating-point number.
  356.      * @since   JDK1.0
  357.      */
  358.     public static native long doubleToLongBits(double value);
  359.  
  360.     /**
  361.      * Returns the double-float corresponding to a given bit represention.
  362.      * The argument is considered to be a representation of a 
  363.      * floating-point value according to the IEEE 754 floating-point 
  364.      * "double precision" bit layout. That floating-point 
  365.      * value is returned as the result. 
  366.      * <p>
  367.      * If the argument is <code>0x7f80000000000000L</code>, the result 
  368.      * is positive infinity. 
  369.      * <p>
  370.      * If the argument is <code>0xff80000000000000L</code>, the result 
  371.      * is negative infinity. 
  372.      * <p>
  373.      * If the argument is any value in the range 
  374.      * <code>0x7ff0000000000001L</code> through 
  375.      * <code>0x7fffffffffffffffL</code> or in the range 
  376.      * <code>0xfff0000000000001L</code> through 
  377.      * <code>0xffffffffffffffffL</code>, the result is NaN. All IEEE 754 
  378.      * NaN values are, in effect, lumped together by the Java language 
  379.      * into a single value. 
  380.      *
  381.      * @param   bits   any <code>long</code> integer.
  382.      * @return  the <code>double</code> floating-point value with the same
  383.      *          bit pattern.
  384.      * @since   JDK1.0
  385.      */
  386.     public static native double longBitsToDouble(long bits);
  387.  
  388.     /* Converts a string to a double.  Also used by Float.valueOf */
  389.     static native double valueOf0(String s) throws NumberFormatException;
  390.  
  391.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  392.     private static final long serialVersionUID = -9172774392245257468L;
  393. }
  394.